package com.zzw.app1;

import android.text.TextUtils;
import android.util.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

public class RSAHelper {
	// 公匙
	private static final String mPublic = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDtNvqrBE3GqbKh76Npy8F02oDO6Md3k5iZsu5qtJ/f"
			+ "n8CCHsryj+9gBYhI5lfFm0giz84tV1V8ONVuvk3fmFlsGT1V1J+stsUuzKebCbI0Tdnwax+YFCLhJmVq3toVcgzDNlT8EMAvO/N"
			+ "Vsb8wbgC2W859jRnO451TGN6+kXiKBQIDAQAB";
	// 私匙
	private static final String mPrivate = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO02+qsE"
			+ "TcapsqHvo2nLwXTagM7ox3eTmJmy7mq0n9+fwIIeyvKP72AFiEjmV8WbSCLPzi1XVXw41W6+Td+YWW"
			+ "wZPVXUn6y2xS7Mp5sJsjRN2fBrH5gUIuEmZWre2hVyDMM2VPwQwC8781WxvzBuALZbzn2NGc7jnVMY3r"
			+ "6ReIoFAgMBAAECgYAf+pBya6McjBYD1K1//rL20NoairERQrmIxuFzSyReXkfygrozVwobNMO5qCve/N8NqlwO"
			+ "Kr7gfzzusAMJiJ1NPehgU0BN/Ac80HFx63j5o7CguLTemtFVL1W0zT5uA7KC9os4iEW7l++jG9jvdLnKsSt5RjvtPj"
			+ "b4WpKLesU4oQJBAPvm/mNMqp8LSvsTDbPrkJ/ZXjZt/Mla+HdoDFbUye9qkmoyarHa1oCGZkcAFvhwWy9JnxSmf9Xz"
			+ "MsRdUa884IkCQQDxEtJPbvZ+Jg+F8j+2LlcMe/AE9CeZ8biBtDOmFzkxqQnpE45mn+y/2iSpA49mZwZVkp6d2iiZO/H"
			+ "AH8acuqadAkAta6xI6WUGgnnFpdzdqUcNm5siQ1jGUdHlbexzp7C1q8i0v1x/r07oI9PDT5/UsiWu9B5sEc2RHWX1BEK"
			+ "bT2XJAkEAt0veSLNQDB6YUvocY0LcwQtDNiEGM1DNPyFq2/BwdkfW1zcdYP5cHouYsEGauPvAnzP6En+butcPO0tt0iFB"
			+ "KQJAID2KUi1VEo5VTfqC2ntkre5yi0iuVNTjWV/z5tNCwtS6LhdIXKIP74oThnc5xjWLMCIJ1D4vrzS2vf/gKW7X8w==";

	/**
	 * 用公钥加密 <br>
	 * 每次加密的字节数，不能超过密钥的长度值减去11
	 * 
	 * @param data
	 *            需加密数据的byte数据
	 * @param pubKey
	 *            公钥
	 * @return 加密后的byte型数据
	 */
	public static String encryptData(String data) {
	/*	if (TextUtils.isEmpty(data)) {
			return null;
		}*/
		try {

			PublicKey publicKey = getPublicKey(mPublic);
			Cipher cipher = Cipher.getInstance("RSA");
			// 编码前设定编码方式及密钥
			cipher.init(Cipher.ENCRYPT_MODE, publicKey);
			// 传入编码数据并返回编码结果
			return Base64.encodeToString(cipher.doFinal(data.getBytes()),
					Base64.NO_WRAP);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 用私钥解密
	 * 
	 * @param encryptedData
	 *            经过encryptedData()加密返回的byte数据
	 * @param privateKey
	 *            私钥
	 * @return
	 */
	public static String decryptData(String encryptedData) {
		if (TextUtils.isEmpty(encryptedData)) {
			return null;
		}
		try {
			byte[] encrypteArr = Base64.decode(encryptedData, Base64.NO_WRAP);
			PrivateKey privateKey = getPrivateKey(mPrivate);
			Cipher cipher = Cipher.getInstance("RSA");
			cipher.init(Cipher.DECRYPT_MODE, privateKey);
			return new String(cipher.doFinal(encrypteArr), "UTF-8");
		} catch (Exception e) {
			
			return null;
		}
	}

	/****************************************
	 * 函数说明：getPublicKey 取得公钥
	 * 
	 * @param key
	 *            公钥字符串
	 * @throws Exception
	 * @return PublicKey 返回公钥
	 * @author zhangmin
	 ***************************************/
	public static PublicKey getPublicKey(String key) throws Exception {
		byte[] keyBytes = Base64.decode(key, Base64.NO_WRAP);

		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PublicKey publicKey = keyFactory.generatePublic(keySpec);
		return publicKey;
	}

	/****************************************
	 * 函数说明：getPrivateKey 取得私钥
	 * 
	 * @param key
	 *            私钥字符串
	 * @throws Exception
	 * @return PrivateKey 返回私钥
	 * @author zhangmin
	 ***************************************/
	public static PrivateKey getPrivateKey(String key) throws Exception {
		byte[] keyBytes = Base64.decode(key, Base64.NO_WRAP);

		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
		return privateKey;
	}
}
